
import { Utils } from '../utils';
import { FormArray } from './FormArray';
import { FormGroup } from './FormGroup';
import { DirtyChangeFn, HiddenChangeFn, ReadonlyChangeFn, ValidChangeFn, ValueChangeFn } from './form.interface';

export class BaseControl {

	// constructor() {
	// }

	/**
	 * @description value 改变时 回调
	 */
	private _valueChangeCallbacks: Array<ValueChangeFn> = [];
	public valueChange = (callback: ValueChangeFn) => {
		// 是不是函数
		const isFunction = Utils.isFunction(callback);
		if(isFunction === false){
			return;
		}
		// 是否包含
		const isInclude = this._valueChangeCallbacks.findIndex((ck) => {
			// return JSON.stringify(ck) === JSON.stringify(callback);
			return ck === callback;
		}) > -1;
		if(isInclude === true){
			return;
		}
		// 添加
		this._valueChangeCallbacks.push(callback);
	};
	protected runValueChangeCallbacks = (value: any) => {
		this._valueChangeCallbacks.forEach((callback) => {
			callback(value);
		});
	};

	/**
	 * @description valid 改变时 回调
	 */
	private _validChangeCallbacks: Array<ValidChangeFn> = [];
	/**
	 * 验证结果发生改变时
	 * @param callback 回调函数
	 */
	public validChange = (callback: ValidChangeFn) => {
		if (Utils.isFunction(callback)) {
			this._validChangeCallbacks.push(callback);
		}
	};
	protected runValidChangeCallbacks = (valid: boolean) => {
		this._validChangeCallbacks.forEach((callback) => {
			callback(valid);
		});
	};

	/**
	 * @description dirty 改变时 回调
	 */
	private _dirtyChangeCallbacks: Array<DirtyChangeFn> = [];
	dirtyChange = (callback: DirtyChangeFn) => {
		if (Utils.isFunction(callback)) {
			this._dirtyChangeCallbacks.push(callback);
		}
	};
	protected runDirtyChangeCallbacks = (dirty: boolean) => {
		this._dirtyChangeCallbacks.forEach((callback) => {
			callback(dirty);
		});
	};


	/** 隐藏 */
	private _hidden = false;
	/** 隐藏改变，回调函数 */
	private _hiddenChangeCallbacks: Array<HiddenChangeFn> = [];
	/** 设置，隐藏改变时的回调 */
	public hiddenChange = (callback: HiddenChangeFn) => {
		if (Utils.isFunction(callback)) {
			this._hiddenChangeCallbacks.push(callback);
		}
	};
	/** 运行，隐藏改变的回调 */
	private _runHiddenChangeCallbacks = (value: boolean) => {
		this._hiddenChangeCallbacks.forEach((callback) => {
			callback(value);
		});
	};
	/** 设置隐藏 */
	public set hidden(val: boolean) {
		// 隐藏发生改变
		if (this._hidden !== val) {
			this._hidden = val;
			this._runHiddenChangeCallbacks(val);
		}
	};
	/** 隐藏 */
	public get hidden() {
		return this._hidden;
	};
	/** 显示 */
	public get show() {
		return !this._hidden
	}
	/** 设置显示 */
	public set show(val: boolean) {
		this.hidden = !val;
	}


	/** 只读改变，回调函数 */
	private _readonlyChangeCallbacks: Array<ReadonlyChangeFn> = [];
	/** 设置，只读改变时的回调 */
	public readonlyChange = (callback: ReadonlyChangeFn) => {
		if (Utils.isFunction(callback)) {
			this._readonlyChangeCallbacks.push(callback);
		}
	
	};
	/** 运行，只读改变的回调 */
	private _runReadonlyChangeCallbacks = (value: boolean) => {
		this._readonlyChangeCallbacks.forEach((callback) => {
			callback(value);
		});
	};
	/** 只读 */
	private _readonly = false;
	/** 设置只读 */
	public set readonly(val: boolean) {
		this.setReadonly(val);
	};
	/** 只读 */
	public get readonly() {
		// 只读例外
		if(this.readonlyException){
			return true;
		}
		return this._readonly;
	};
	/** 设置只读 */
	public setReadonly = (val: boolean) => {
		// console.log(val);
		// 只读例外
		if(this.readonlyException){
			return;
		}
		// 只读发生改变
		if (this._readonly !== val) {
			this._readonly = val;
			this._runReadonlyChangeCallbacks(val);
		}
	};





	
	/** 只读例外 */
	public readonlyException: boolean = false;
	/**
	 * @description 父级Form对象
	 */
	public parent: FormArray | FormGroup | null = null;
	
	/** 任意数据 */
	public data:any = {};
}
